package com.android.lovely.manager

import android.text.format.DateUtils
import com.android.lovely.data.LovelyAdId
import com.android.lovely.user.UserManager
import com.android.lovely.util.MMKVUtils
import com.kotlin.extensions.json.JsonExt.fromJson
import com.kotlin.extensions.json.JsonExt.toJson

/**
 *
 * @author why
 * @date 2025/3/8 01:42
 */
class WorkStore private constructor() {

    private val dataStore = MMKVUtils.instance.create("LOVELY_WORK")

    private var lastTime: Long = dataStore.decode("YriE1GQ2MoyVIg9t", 0)
        set(value) {
            field = value
            dataStore.encode("YriE1GQ2MoyVIg9t", value)
        }

    fun check() {
        if (DateUtils.isToday(lastTime).not()) {
            lastTime = System.currentTimeMillis()
            buyDialogShowCount = 0
            buyPageShowCount = 0
            UserManager.refreshAuth()
        }
    }

    var lovelyAdId: LovelyAdId? = dataStore.decode("9qGa97DEOH4ia7f4", "").fromJson()
        set(value) {
            field = value
            dataStore.encode("9qGa97DEOH4ia7f4", value.toJson())
        }

    //充值弹窗广告展示次数
    var buyDialogShowCount: Int = dataStore.decode("er6qrVqDt8Abpb7b", 0)
        set(value) {
            field = value
            dataStore.encode("er6qrVqDt8Abpb7b", value)
        }

    //充值页广告展示次数
    var buyPageShowCount: Int = dataStore.decode("Oz9BdcLHpmMyq2Bd", 0)
        set(value) {
            field = value
            dataStore.encode("Oz9BdcLHpmMyq2Bd", value)
        }

    companion object {
        @Volatile
        private var instance: WorkStore? = null

        fun getInstance(): WorkStore {
            val advertise = instance ?: synchronized(this) {
                instance ?: WorkStore().also { instance = it }
            }
            advertise.check()
            return advertise
        }
    }
}